<!--
  * 分润提现表
  *
  * @Author:    zhoujk
  * @Date:      2025-01-23 04:08:27
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-col :span="4">
        <a-form-item label="提现金额" class="smart-query-form-item">
          <a-input v-model:value="queryForm.drawAmt" placeholder="提现金额" /> </a-form-item
      ></a-col>
      <a-col :span="6">
        <a-form-item label="提现日期" class="smart-query-form-item">
          <a-range-picker
            :allowClear="false"
            :inputReadOnly="true"
            class="w-full"
            v-model:value="queryForm.drawTime"
            @change="onChangeCTime"
          /> </a-form-item
      ></a-col>
      <a-col :span="4">
        <a-form-item label="审核状态" class="smart-query-form-item">
          <a-select v-model:value="queryForm.auditStatus" allowClear :options="PROFIT_AUDIT_STATUS" placeholder="请选择审核状态" /> </a-form-item
      ></a-col>
      <a-col :span="4">
        <a-form-item label="代理商名称" class="smart-query-form-item">
          <a-input v-model:value="queryForm.agentNameLike" placeholder="输入代理商名称" />
        </a-form-item>
      </a-col>
      <!-- <a-col :span="3">
        <a-form-item label="所属代理商" class="smart-query-form-item">
          <a-input v-model:value="queryForm.fAgentNameLike" placeholder="输入所属代理商" />
        </a-form-item>
      </a-col> -->
      <a-col :span="5">
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item></a-col
      >
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-setting-block">
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button v-if="4 == record.auditStatus" @click="showForm(record, true)" type="link">审核</a-button>
            <a-button
              v-privilege="'profit:draw:find_id'"
              v-if="3 == record.auditStatus || 2 == record.auditStatus"
              @click="showForm(record, false)"
              type="link"
              >查看</a-button
            >
            <!-- <a-button v-if="1 == record.auditStatus || 0 == record.auditStatus" @click="handlePayment(record)" type="link">打款</a-button> -->
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>

    <ProfitDrawsForm ref="formRef" @reloadList="queryData" />
  </a-card>
</template>
<script setup>
  import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import { ZZB_AUDIT_STATUS_ENUM } from '/@/constants/system/system-const.js'
  import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
  import { profitDrawApi } from '/@/api/business/b-agent/profit/profit-draw/profit-draw-api'
  import { PAGE_SIZE_OPTIONS } from '/src/constants/common-const'
  import { smartSentry } from '/src/lib/smart-sentry'
  import TableOperator from '/src/components/support/table-operator/index.vue'
  import { defaultTimeRanges } from '/src/lib/default-time-ranges'
  import ProfitDrawsForm from './profit-draw-form.vue'
  import dayjs from 'dayjs'
  import { formatToMoney, selectDictLabel } from '/@/utils/common'
  import {
    PROFIT_AUDIT_STATUS, //审核状态
  } from '/@/constants/dict'

  const { proxy } = getCurrentInstance()
  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      customRender: ({ index }) => {
        return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
      },
    },
    {
      title: '提现日期',
      dataIndex: 'drawTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD'),
      width: 180,
      ellipsis: true,
    },
    {
      title: '代理商名称',
      dataIndex: 'companyName', //高说取companyName
      ellipsis: true,
    },
    // {
    //   title: '所属代理商',
    //   dataIndex: 'fAgentName',
    //   ellipsis: true,
    // },
    {
      title: '联系人',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '联系方式',
      dataIndex: 'phone',
      ellipsis: true,
    },
    {
      title: '期初金额',
      dataIndex: 'sAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '提现金额',
      dataIndex: 'drawAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    // {
    //   title: '期末金额',
    //   dataIndex: 'eAmt',
    //   customRender: (column) => formatToMoney(column.text / 100),
    //   ellipsis: true,
    // },
    {
      title: '手续费（1‰）',
      dataIndex: 'fee',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '代扣税费（7%）',
      dataIndex: 'holdFee',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    // {
    //   title: '实际到账金额',
    //   dataIndex: 'holdFee',
    //   ellipsis: true,
    // },
    {
      title: '状态',
      dataIndex: 'auditStatus',
      customRender: ({ text }) => {
        return selectDictLabel(PROFIT_AUDIT_STATUS, text)
      },
      ellipsis: true,
    },
    {
      title: '审核失败原因',
      dataIndex: 'rejectReason',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 90,
    },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    drawAmt: undefined, //提现金额
    drawTime: [dayjs().startOf('month'), dayjs().endOf('month')], //提现日期
    drawTimeBegin: dayjs().startOf('month').valueOf(), //提现日期 开始
    drawTimeEnd: dayjs().endOf('month').valueOf(), //提现日期 结束
    auditStatus: undefined, //审核状态
    agentNameLike: undefined, //代理商
    fAgentNameLike: undefined, //代理商
    drawAuditFlag: true, //是否为提现审核
    pageNo: 1,
    pageSize: 10,
  }
  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      const param = { ...queryForm, drawAmt: queryForm.drawAmt * 100 }
      let queryResult = await profitDrawApi.page(param)
      // queryResult.data.forEach((e) => {
      //   e.sAmt = (e.sAmt / 100).toFixed(2)
      //   e.drawAmt = (e.drawAmt / 100).toFixed(2)
      //   e.eAmt = (e.eAmt / 100).toFixed(2)
      //   e.fee = (e.fee / 100).toFixed(2)
      //   e.holdFee = (e.holdFee / 100).toFixed(2)
      // })
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  function onChangeCTime(dates) {
    queryForm.cTimeBegin = dates[0].startOf('days').valueOf()
    queryForm.cTimeEnd = dates[1].endOf('days').valueOf()
  }

  onMounted(queryData)

  // ---------------------------- 添加/修改 ----------------------------
  const formRef = ref()

  function showForm(data, auditFlag) {
    formRef.value.show(data, auditFlag)
  }
  // 打款
  // function handlePayment(row) {
  //   Modal.confirm({
  //     title: '确认打款',
  //     content: `确认${row.agentName}的提现金额已打款，请认真校对！`,
  //     okText: '确定',
  //     okType: 'danger',
  //     async onOk() {
  //       profitDrawApi.payment(row.id).then(() => {
  //         message.success('打款成功！')
  //         queryData()
  //       })
  //     },
  //     cancelText: '取消',
  //     onCancel() {},
  //   })
  // }
</script>
